<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of menuedit
 *
 * @author root
 */
class menuedit extends admintemp {
    
    public $dba;
    
    public function __construct() {
        parent::__construct();
        if(!isset($_SESSION['authpd'])){
            redirect(WEBPATH);
        }
        
        $this->dba=new db();
    }
    
    public function index(){
        //获取SQL查询条件 parent_id,如果这个参数为空则赋值为 0,表未查询主菜单
        $currentmenu = $_REQUEST['currentmenu'];
        if (empty($currentmenu)) {
            $currentmenu = '0';
        }
        //如果是查询主菜单,则显示中文为 '主菜单',否则显示parent_id的对应name值
        if($currentmenu == '0'){
            $currentmenuname = '主菜单';
        }else{
            $currentmenuname_temp = $this->dba->fetch_arrone("select id,name,parent_id from channel where id='$currentmenu'");
            $currentmenuname = $currentmenuname_temp['name'];
        }
        //如果用户点击了'保存排序'按钮
        if($_POST['clicksort']){
            //分割获取到的排序用的参数为PHP数组
            $str=$_POST['list1SortOrder'];
            $str=(explode("||",$str));
            $data1=(explode("|",$str[0]));
            $data2=(explode("|",$str[1]));
            foreach ($data1 as $key=>$value) {
                $this->dba->query("update channel set sort='$key' where id='$value'");
                //echo '<p>'.$value.'/'.$key.'</p>';
            }
        }else if($_POST['clickadd']){
            $name=$_POST['menuname'];
            $type=$_POST['type'];
            $content=$_POST['myselect1'];
            $parent_id=$_POST['currentmenu'];
            
            $sort = $this->dba->fetch_all("select count(*) as sum from channel where parent_id=\"$parent_id\"");
            $sort = $sort[0]['sum']+1;
            
            $sql = "insert into channel (name,type,parent_id,content,sort) values (\"$name\",\"$type\",\"$parent_id\",\"$content\",\"$sort\")";
            $this->dba->query($sql);
        }
        
        //显示菜单及对模板赋值相应的变量
        $data = $this->dba->fetch_all("select id,sort,name from channel where parent_id='$currentmenu' order by sort");
        //查询 返回上级菜单 需要的  ID
        $parentmenu_temp = $this->dba->fetch_arrone("select id,parent_id from channel where id='$currentmenu'");
        $parentmenu_temp = $parentmenu_temp['parent_id'];
        $parentmenu_temp2 = $this->dba->fetch_arrone("select id,parent_id from channel where id='$parentmenu_temp'");
        if ($parentmenu_temp2==NULL) {
            $parentmenu='0';
        }else{
            $parentmenu = $parentmenu_temp2['id'];
        }
        //菜单排序之前的ID顺序
        foreach ($data as $key => $value) {
            $str .= $value[sort];
            $str.=count($data)-1 != $key ? '|' :null;
        }
        $this->assign("olddata", $str);//菜单排序之前的ID顺序
        $this->assign("row", $data);
        //显示所有单页面
        $data = $this->dba->fetch_all("select * from SinglePage;");
        $this->assign("contentdata", $data);
        
        $this->assign("parentmenu", $parentmenu);
        $this->assign("currentmenuval", $currentmenu);
        $this->assign("currentmenuname", $currentmenuname);
        $this->display("include/menuedit.html");
    }
    
    public function editmenu(){
        if($_POST['click']){
            $id = $_REQUEST['editsonmenuid'];
            $name = $_REQUEST['menuname'];
            $type = $_REQUEST['type'];
            $content = $_REQUEST['myselect1'];
            
            $sql = "update channel set `name`=\"$name\",`type`=\"$type\",`content`=\"$content\" where `id`=\"$id\"";
            //echo $sql;
            if($this->dba->query($sql)){
                redirect("?c=menuedit");
            }
        }else{
            $id=$_REQUEST['id'];
            $sonmenu = $this->dba->fetch_arrone("select * from channel where `id`=\"$id\"");
            //显示所有单页面
            $data = $this->dba->fetch_all("select * from SinglePage;");
            $this->assign("contentdata", $data);
            
            $this->assign("editsonmenutype", $sonmenu['type']);
            $this->assign("editsonmenuid", $sonmenu['id']);
            $this->assign("editsonmenuname", $sonmenu['name']);
            
            $singlepageid = $sonmenu['content'];
            $data = $this->dba->fetch_arrone("select id,title from SinglePage where `id`=\"$singlepageid\"");
            
            $this->assign("editsonmenu_singlepagetitle", $data['title']);
            $this->assign("editsonmenu_singlepageid", $data['id']);
            
            $this->display("include/editsonmenu.html");
        }
    }
    
    public function delmenu(){
        $menuid=$_REQUEST['id'];
        if($this->dba->query("delete from channel where id=\"$menuid\"")){
            $this->index();
        }
        
    }
}

?>
